import router from '@/router'
import store from '@/store'
import { Message } from 'element-ui'
import { getToken } from '@/utils/token'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({showSpinner:false})
const whiteList = ['/login','/404']
router.beforeEach((to, from, next) => {
   // console.log(to.path);
   NProgress.start()
	if(getToken()){
		if(to.path === '/login'){
			next({path:'/'})
			NProgress.done()
		}else{
			store.dispatch('GetInfo').then(res => {
				
				store.dispatch('GenerateRoutes').then(accessRoutes => {
					router.addRoutes(accessRoutes)
				})
			}).catch(err=>{
				store.dispatch('loginOut').then((err)=>{
					Message.error(err)
					next({path:'/'})
				})
			})
			next();
		}
	}else{
		if(whiteList.indexOf(to.path)!==-1){
			next()
		}else{
			next(`/login?redirect=${to.fullPath}`)
			NProgress.done();
		}
	}
})
router.afterEach(() => {
	NProgress.done()
  })